package com.example.springbootmybatisplus.sjms;

import com.example.springbootmybatisplus.config.LoginTypeConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName UserLoginFactory
 * @Description: TODO
 * * 操作策略的上下文环境类 工具类
 * * 将策略整合起来 方便管理
 * @Author: Sunny
 * @Create: 2024-08-18 20:30
 * @Version 1.0
 **/
@Component
public class UserLoginFactory implements ApplicationContextAware {
    private static Map<String, UserGranter> granterPool = new ConcurrentHashMap<>();

    @Autowired
    private LoginTypeConfig loginTypeConfig;
    /**
     * 从配置文件中读取策略信息存储到map中
     * {在application.yml文件中新增自定义配置
     新增读取数据配置类
     * account:accountGranter,
     * sms:smsGranter,
     * we_chat:weChatGranter
     * }
     * *
     @param applicationContext
      * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        loginTypeConfig.getTypes().forEach((k, y) -> {
            granterPool.put(k, (UserGranter)
                    applicationContext.getBean(y));
        });
    }


/**
 * 对外提供获取具体策略
* *
 * @param grantType 用户的登录方式，需要跟配置文件中匹配
* @return 具体策略
*/
    public UserGranter getGranter(String grantType) {
        UserGranter tokenGranter = granterPool.get(grantType);
        return tokenGranter;
    }
}